package com.first.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.TbZone;
import com.first.common.vo.Result;
import com.first.system.dao.ZoneDao;
import com.first.system.service.ZoneService;
import com.first.system.vo.ZoneVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-05-26 16:05
 **/
@Service
public class ZoneServiceImpl implements ZoneService {
    @Autowired
    ZoneDao zoneDao;

    @Override
    public Result getAllZone(Integer currentPage, Integer pageSize, ZoneVo zoneVo) {
        Page<TbZone> page = new Page<>(currentPage, pageSize);
        QueryWrapper<TbZone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if (StringUtils.hasText(zoneVo.getZoneName())) {
            wrapper.like("zone_name", zoneVo.getZoneName());
        }
        if (zoneVo.getIsDisable() != null && zoneVo.getIsDisable() != "") {
            wrapper.eq("is_disable", zoneVo.getIsDisable());
        }
        Page<TbZone> page1 = zoneDao.selectPage(page, wrapper);
        return Result.success(page1);
    }


    @Override
    public Result addZone(TbZone tbZone) {
        if (StringUtils.hasText(tbZone.getId())) {
            zoneDao.updateById(tbZone);
            return Result.success();
        }
        TbZone tbZone1 = new TbZone(IdUtil.getSnowflakeNextId() + "", LocalDateTime.now(), LocalDateTime.now(), 0, 0, tbZone.getZoneName(), tbZone.getZoneDesc(),new ArrayList<>());
        int insert = zoneDao.insert(tbZone1);
        if (insert != 0) {
            return Result.success();
        }
        return null;
    }

    @Override
    public Result changeStatus(String id, Integer isDisable) {
        TbZone tbZone = new TbZone();
        tbZone.setId(id);
        tbZone.setIsDisable(isDisable);
        int i = zoneDao.updateById(tbZone);
        if (i != 0) {
            return Result.success();
        }
        return null;
    }

    @Override
    public Result deleteZone(TbZone tbZone) {
        tbZone.setIsDeleted(1);
        int i = zoneDao.updateById(tbZone);
        if (i != 0) {
            return Result.success();
        }
        return null;
    }

}
